package ui;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import entity.general.MinTime;

public class AddSubTimeDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField hoursField;
	private JTextField minutesField;
	private JRadioButton rdbtnAdd;
	private JRadioButton rdbtnSub;
	static public enum Status {OK, CANCEL};

	Status status;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			AddSubTimeDialog dialog = new AddSubTimeDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public AddSubTimeDialog() {
		this.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
		setIconImage(Toolkit.getDefaultToolkit().getImage(AddSubTimeDialog.class.getResource("/images/ActivlyIcon.png")));
		setTitle("Add/Sub Time");
		setBounds(100, 100, 194, 148);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{90, 0};
		gbl_contentPanel.rowHeights = new int[]{30, 0, 0};
		gbl_contentPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_contentPanel.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		contentPanel.setLayout(gbl_contentPanel);
		{
			JPanel panel = new JPanel();
			GridBagConstraints gbc_panel = new GridBagConstraints();
			gbc_panel.fill = GridBagConstraints.BOTH;
			gbc_panel.insets = new Insets(0, 0, 5, 0);
			gbc_panel.gridx = 0;
			gbc_panel.gridy = 0;
			contentPanel.add(panel, gbc_panel);
			{
				hoursField = new JTextField();
				hoursField.setText("00");
				panel.add(hoursField);
				hoursField.setColumns(2);
			}
			{
				JLabel lblH = new JLabel("h :");
				panel.add(lblH);
			}
			{
				minutesField = new JTextField();
				minutesField.setText("00");
				panel.add(minutesField);
				minutesField.setColumns(2);
			}
			{
				JLabel lblM = new JLabel("m");
				panel.add(lblM);
			}
		}
		{
			JPanel panel = new JPanel();
			GridBagConstraints gbc_panel = new GridBagConstraints();
			gbc_panel.fill = GridBagConstraints.BOTH;
			gbc_panel.gridx = 0;
			gbc_panel.gridy = 1;
			contentPanel.add(panel, gbc_panel);
			
			ButtonGroup group = new ButtonGroup();
			{
				rdbtnAdd = new JRadioButton("Add");
				rdbtnAdd.setSelected(true);
				panel.add(rdbtnAdd);
				group.add(rdbtnAdd);
			}
			{
				rdbtnSub = new JRadioButton("Sub");
				panel.add(rdbtnSub);
				group.add(rdbtnSub);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(new ActionListener()
				{
					@Override
					public void actionPerformed(ActionEvent arg0) {
						System.out.println("Pressed OK");
						status = Status.OK;
						AddSubTimeDialog.this.dispose();
					}	
				}
			);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(new ActionListener()
				{
					@Override
					public void actionPerformed(ActionEvent arg0) {
						status = Status.CANCEL;
						AddSubTimeDialog.this.dispose();
					}	
				}
				);
				buttonPane.add(cancelButton);
			}
			
		}
	}

	public MinTime getTime()
	{
		int hrs = Integer.valueOf(hoursField.getText());
		int mins = Integer.valueOf(minutesField.getText());		
		return new MinTime(hrs, mins);
	}
	
	public boolean isAdd()
	{
		return rdbtnAdd.isSelected();
	}

	public Status getStatus() {
		return status;
	}
	
}
